package com.SmartLED.Service;

import com.SmartLED.Common.SYS_Base;
import com.SmartLED.Mapper.BaseMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by Frozen on 2017/1/9.
 */

@SpringBootApplication
@Component
public class BaseMapperService extends SYS_Base{

    @Autowired
    private BaseMapper baseMapper;

    // TODO: 2017/1/9  所有的需要传 model 的方法,后期可以用传 map 来代替,现在用 model 主要是因为需要通过 model 来获取表名
    public long save(Object object){
        Map resultMap = new HashMap();
        final Map params = new ObjectMapper().convertValue(object,Map.class);
        if (params.containsKey("id") && params.get("id") != null)
        {
            //修改
            resultMap.put("tableName",queryClassName(object.getClass().getName()));
            resultMap.put("condition",new HashMap<String,Object>(){{put("id",params.get("id"));}});
            resultMap.put("relation",handleObject(object));
            return baseMapper.update(resultMap);
        }
        else
        {
            //新增
            resultMap.put("tableName", queryClassName(object.getClass().getName()));
            resultMap.put("relation",handleObjectForSave(object));
            return baseMapper.insertWithoutBackId(resultMap);
        }
    }


    public long update(Object object,Map condition)
    {
        Map resultMap = new HashMap();
        resultMap.put("tableName",queryClassName(object.getClass().getName()));
        resultMap.put("condition",condition);
        resultMap.put("relation",handleObject(object));
        return baseMapper.update(resultMap);
    }


    public List find(Object object,List field,Map orderBy)
    {
        Map resultMap = new HashMap();
        resultMap.put("tableName",queryClassName(object.getClass().getName()));
        resultMap.put("condition",handleObject(object));
        resultMap.put("orderBy",orderBy);
        return baseMapper.find(resultMap);
    }


    public long delete(Object object)
    {
        Map resultMap = new HashMap();
        resultMap.put("tableName",queryClassName(object.getClass().getName()));
        resultMap.put("condition",handleObject(object));
        return baseMapper.delete(resultMap);
    }

    /**
     * 添加创建者和创建时间
     * @param params
     */
    private void addCreateBy(Map params)
    {
        if (params.containsKey("create_date"))
        {
            params.put("create_date",new Date());
            params.put("operator_id",operator_id);
        }
    }


    /**
     * 添加修改者和修改时间
     * @param params
     */
    private void addUpdateBy(Map params)
    {
        if (params.containsKey("update_date"))
        {
            params.put("update_date",new Date());
            params.put("operator_id",operator_id);
        }
    }


    /**
     * 根据获取到的类型,拿到实际的类型
     * @param className
     * @return
     */
    private String queryClassName(String className)
    {
        return className.split("\\.")[className.split("\\.").length - 1];
    }

    /**
     * 处理数据
     * @param object
     * @return
     */
    private Map handleObject(Object object)
    {
        Map<String,Object> params = new ObjectMapper().convertValue(object,Map.class);  //将 model 转成 map
        addUpdateBy(params);
        List<String> deleteKey = new ArrayList<String>(); //保存需要删除的 key
        //遍历 map 获取哪些字段的值是 Null
        for (Map.Entry<String,Object> entry : params.entrySet())
        {
            if (entry.getValue() == null)
            {
                deleteKey.add(entry.getKey());
            }
        }
        //删除选中的 key
        for (String key : deleteKey)
        {
            params.remove(key);
        }
        return params;
    }


    /**
     * 新增时处理数据
     * TODO 主要问题是 addCreateBy 方法调用问题,还有删除多余字段时,删除 ID 的问题,后期进行代码合并与优化
     * @param object
     * @return
     */
    private Map handleObjectForSave(Object object)
    {
        Map<String,Object> params = new ObjectMapper().convertValue(object,Map.class);  //将 model 转成 map
        addCreateBy(params);
        addUpdateBy(params);
        List<String> deleteKey = new ArrayList<String>(); //保存需要删除的 key
        //遍历 map 获取哪些字段的值是 Null
        for (Map.Entry<String,Object> entry : params.entrySet())
        {
            if (entry.getValue() == null || entry.getKey().equals("id"))
            {
                deleteKey.add(entry.getKey());
            }
        }
        //删除选中的 key
        for (String key : deleteKey)
        {
            params.remove(key);
        }
        return params;
    }
}
